package com.loren.demo21.controller;

import com.loren.demo21.task.TaskRegister;
import com.loren.demo21.task.TaskRunnable;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/task")
public class TaskController {

    private final TaskRegister taskRegister;

    @GetMapping("/add")
    public String addTask() {
        this.taskRegister.addCronTask(new TaskRunnable("taskService", "task03"), "0/5 * * * * ?");
        return "success";
    }

    @GetMapping("/update")
    public String updateTask() {
        // 移除原有的
        this.taskRegister.removeCronTask(new TaskRunnable("taskService", "task01"));
        // 添加新的
        this.taskRegister.addCronTask(new TaskRunnable("taskService", "task01"), "0/2 * * * * ?");
        return "success";
    }

    @GetMapping("/delete")
    public String deleteTask(@RequestParam("name") String name) {
        // 移除原有的
        this.taskRegister.removeCronTask(new TaskRunnable("taskService", name));
        return "success";
    }

}
